﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>组织模块管理</title>
    <link href="~/Content/css/fontawesome/css/font-awesome.min.css" rel="stylesheet" />
    <link href="~/Content/easyui/themes/default/easyui.css" rel="stylesheet" />
    <link href="~/Content/easyui/themes/icon.css" rel="stylesheet" />
    <!--修改过的bootstrap-->
    <link href="~/Content/css/bootstrapmin.css" rel="stylesheet" />
</head>
<body>
    <div class="easyui-layout" fit="true">
        @*左边导航栏*@
        <div data-options="region:'west',split:true" style="width: 200px;">
            <div id="tree">
            </div>
        </div>
        @*中间内容*@
        <div data-options="region:'center',border:false,split:false">
            <div id="maingrid">
            </div>
        </div>
    </div>
    <div id="maindialog" style="padding:10px;">
        <form class="form-horizontal">
            <div class="form-group">
                <label for="txtOrgName" class="col-md-2 control-label">组织名称：</label>
                <div class="col-md-4">
                    <input type="text" class="form-control" id="txtOrgName" />
                </div>
                <label for="txtOrgParentId" class="col-md-2 control-label">父组织：</label>
                <div class="col-md-4">
                    <input type="text" class="form-control" id="txtOrgParentId" />
                </div>
            </div>
            <div class="form-group">
                <label for="txtOrgDesc" class="col-md-2 control-label">组织描述： </label>
                <div class="col-md-10">
                    <textarea class="form-control" id="txtOrgDesc" rows="2"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label for="txtSortNo" class="col-md-2 control-label">排序号： </label>
                <div class="col-md-4">
                    <input type="number" class="form-control" id="txtSortNo" />
                </div>
                <label for="ddlIsAutoExpend" class="col-md-2 control-label">是否展开：</label>
                <div class="col-md-4">
                    <select class="form-control" id="ddlIsAutoExpend">
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="txtIconName" class="col-md-2 control-label">图标： </label>
                <div class="col-md-4">
                    <input type="text" class="form-control" id="txtIconName" />
                </div>
                <label for="ddlIsEnable" class="col-md-2 control-label">是否可用：</label>
                <div class="col-md-4">
                    <select class="form-control" id="ddlIsEnable">
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </select>
                </div>
            </div>
        </form>
    </div>
    <script src="~/Content/js/jquery-2.2.3.min.js"></script>
    <script src="~/Content/easyui/jquery.easyui.min.js"></script>
    <script src="~/Content/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script>
        $(function () {
            var parentId = 0;
            bindgrid();
            bindtree(parentId);
            binddialog();
            bindComTree(parentId);

            bindValidate();
        });
        function bindgrid() {
            $("#maingrid").datagrid({
                nowrap: true,
                fit: true,
                fitColumns: true,
                autoRowHeight: true,
                striped: true,//是否显示斑马线效果
                //singleSelect: true, //是否單選
                //collapsible: true,//是否可折叠
                pagination: true, //分頁控制
                pageSize:20,
                rownumbers: true, //行號
                //showFooter: true,
                method: 'get',
                url: '/OrgManage/GetOrgInfos?t=' + Math.random(),
                //queryParams: {
                //    name: 'easyui',
                //    subject: 'datagrid'
                //},
                loadMsg: '数据正在加载...',
                columns: [[
                    { field: 'ck', checkbox: true },
                    { field: 'OrgId', title: '组织ID', width: 100 },
                    { field: 'OrgName', title: '组织名字', width: 100 },
                    { field: 'OrgDesc', title: '组织描述', width: 130 },
                    { field: 'ParentName', title: '父组织', width: 100 },
                    { field: 'SortNo', title: '排序号', width: 50 },
                    {
                        field: 'IconName', title: '图标', width: 100, formatter: function (value, row, index) {
                            return "<i class='"+value+"'></i>";
                        }
                    },
                    {
                        field: 'IsEnable', title: '是否可用', width: 60, formatter: function (value, row, index) {
                            return value=="1"?"是":"否";
                        }
                    }
                ]],
                toolbar: [{
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        $("#maindialog").dialog("open");
                        $("#txtOrgName").val("");
                        $("#txtOrgParentId").combotree("reset");
                        $("#txtOrgDesc").val("");
                        $("#txtSortNo").val("");
                        $("#ddlIsAutoExpend").val("1");
                        $("#txtIconName").val("");
                        $("#ddlIsEnable").val("1");
                    }
                }, '-', {
                    text: '修改',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var rowData = $('#maingrid').datagrid("getSelected");
                        if (rowData != null) {
                            $("#maindialog").dialog("open");
                            $("#maindialog").data("OrgId", rowData.OrgId);
                            $("#txtOrgName").val(rowData.OrgName);
                            //var tree = $("#txtOrgParentId").combotree("tree");
                            $('#txtOrgParentId').combotree('setValue', rowData.ParentOrgId);
                            //var treeNode = tree.tree("find", rowData.ParentOrgId);
                            //tree.tree("select",treeNode.target);
                            $("#txtOrgDesc").val(rowData.OrgDesc);
                            $("#txtSortNo").val(rowData.SortNo);
                            $("#ddlIsAutoExpend").val(rowData.IsAutoExpend);
                            $("#txtIconName").val(rowData.IconName);
                            $("#ddlIsEnable").val(rowData.IsEnable);
                        } else {
                            alertMsg("请至少选择一项！");
                        }
                    }
                }, '-', {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var rows = $('#maingrid').datagrid("getSelections");
                        if (rows.length > 0) {
                            $.messager.confirm('确认对话框', '确定要删除这条记录？', function (r) {
                                if (r) {
                                    deleteOrg(rows);
                                }
                            });
                        } else {
                            alertMsg("请至少选择一项！");
                        }
                    }
                }, '-', {
                    text: '启用/弃用',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var rowData = $('#maingrid').datagrid("getSelected");
                        if (rowData != null) {
                            $.messager.confirm('确认对话框', '确定要修改这条记录？', function (r) {
                                if (r) {
                                    isEnableOrg(rowData);
                                }
                            });
                        } else {
                            alertMsg("请至少选择一项！");
                        }
                    }
                }, '-', {
                    text: '查看全部',
                    iconCls: 'icon-remove',
                    handler: function () {
                        $("#maingrid").datagrid("reload", {
                            parentId: -1
                        });
                    }
                }]
            });
        }
        function bindtree(parentId) {
            $('#tree').tree({
                method: "get",
                checkbox: false,
                url: "/OrgManage/GetSysOrgsTree?parentId=" + parentId + '&t=' + Math.random()
                , onClick: function (node) {
                    var id = node.id;
                    $("#maingrid").datagrid("reload", {
                        parentId: id
                    });
                }
            });
        }
        function binddialog() {
            $("#maindialog").dialog({
                title: "保存组织",
                //top: 10,
                //left: ($(window).width() - 600) / 2,
                //width: 800,
                //height: 450,
                inline: true, //定义如何布局窗口，如果设置为true，窗口将显示在它的父容器中，否则将显示在所有元素的上面
                iconCls: 'icon-save',
                modal: true,
                draggable: true, //定义是否能够拖拽窗口。
                closed: true,
                buttons: [{
                    text: '保存',
                    iconCls: 'icon-ok',
                    handler: function () {
                        saveOrg();
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        $("#maindialog").dialog("close");
                        $('#maingrid').datagrid("reload");
                    }
                }]
            });
        }
        function bindComTree(parentId) {
            $("#txtOrgParentId").combotree({
                width:"80%",
                method: "get",
                checkbox: false,
                url: "/OrgManage/GetSysOrgsTree?parentId=" + parentId + '&t=' + Math.random()
            });
        }
        function bindValidate() {
            $("#txtOrgName").validatebox({
                required: true,
                validType:"length[1,20]",
                missingMessage: "组织名称不能为空",
            });
        }
        function isValid() {
            return $("#txtOrgName").validatebox("isValid");
        }
        function saveOrg() {
            if (!isValid()) { return false;}
            var OrgId=$("#maindialog").data("OrgId");
            var OrgName = $("#txtOrgName").val();
            var tree = $('#txtOrgParentId').combotree('tree');	// 获取树对象
            var treeNode = tree.tree('getSelected');
            var ParentOrgId = treeNode.id;
            var ParentName = treeNode.text;
            var OrgDesc = $("#txtOrgDesc").val();
            var SortNo = $("#txtSortNo").val();
            if (!SortNo) { SortNo = 0; }
            var IsAutoExpend = $("#ddlIsAutoExpend").val();
            var IconName = $("#txtIconName").val();
            var IsEnable = $("#ddlIsEnable").val();
            var obj = {
                OrgId:OrgId,
                OrgName: OrgName,
                ParentOrgId: ParentOrgId,
                ParentName:ParentName,
                OrgDesc: OrgDesc,
                SortNo: SortNo,
                IsAutoExpend: IsAutoExpend,
                IconName: IconName,
                IsEnable: IsEnable
            };
            $.post("/OrgManage/Add", obj, function (data) {
                if (data.CodeId == "100") {
                    window.location.reload();
                } else {
                    alertMsg("保存失败！");
                }
            });
            return true;
        }
        function deleteOrg(rows) {
            if (rows.length > 0) {
                var ids = "";
                for (var i = 0; i < rows.length; i++) {
                    var treeNode = $('#tree').tree("find", rows[i].OrgId);
                    var children = $("#tree").tree("getChildren", treeNode.target);
                    if (children.length > 0) {
                        alertMsg("该节点包含子节点，请先删除子节点！");
                        return false;
                    }
                    ids = ids + rows[i].OrgId + ",";
                }
                $.post("/OrgManage/Delete", {
                    ids: ids
                }, function (data) {
                    if (data.CodeId == "100") {
                        window.location.reload();
                    } else {
                        alertMsg("删除失败！");
                    }
                });

            }
        }
        function isEnableOrg(row) {
            if (row) {
                $.post("/OrgManage/UpdateIsEnable", {
                    OrgId: row.OrgId
                }, function (data) {
                    if (data.CodeId == "100") {
                        $("#maingrid").datagrid("reload");
                    } else {
                        alertMsg("修改失败！");
                    }
                });

            }
        }
        function alertMsg(msg) {
            $.messager.alert('提示', msg, 'info');
        }
    </script>
</body>
</html>
